import { usePageRequest, useRequest } from "../../../utils/request";

Component({
    properties: {},
    data: {
        // 搜索数据
        city: '',
        start: 0,
        end: 0,
        value: '',
        // 选项
        sortOptions: [
            { text: '最新房源', value: 0 },
            { text: '低价房源', value: 1 },
            { text: '高价房源', value: 2 },
        ],
        roomNum: [
            { name: '一室', value: 1 },
            { name: '二室', value: 2 },
            { name: '三室', value: 3 },
            { name: '四室', value: 4 },
            { name: '五室', value: 5 },
        ],
        sortType: 'new',
        sort: 'asc'
    },
    methods: {
        onLoad(options) {
            this.setData({
                ...options
            })
            this.searchRequest = new usePageRequest('api/housesearch', {
                params: {
                    city: options.city,
                    start_live_time: options.start,
                    end_live_time: options.end,
                    title: options.value
                },
                loading: 'listLoading'
            }, this)

            this.dataRequest = new useRequest('api/housebaseset', {
                data: 'houseData',
                pretreatment: data => {
                    data.house_types = Object.keys(data.house_types).map(key => {
                        return {
                            value: key,
                            name: data.house_types[key].name
                        }
                    })
                    return data
                }
            }, this)
        },
        back() {
            wx.navigateBack()
        },
        // 选择排序
        changeFilter() {
            this.selectComponent('#filter').toggle();
            this.search()
        },
        changeSort(e) {
            const i = e.detail
            this.setData({
                sortType: i === 0 ? 'new' : 'price',
                sort: i === 2 ? 'desc' : 'asc',
            })
            this.search()
        },
        // 选择筛选
        changeFilterItem(e) {
            const { index, type } = e.currentTarget.dataset
            this.setData({
                ['houseData.' + type + '[' + index + '].change']: !this.data.houseData[type][index].change
            })
        },
        changeRoomNum(e) {
            const { index } = e.currentTarget.dataset
            this.setData({
                ['roomNum[' + index + '].change']: !this.data.roomNum[index].change
            })
        },
        cleanFilter() {
            this.setData({
                ['houseData.house_types']: this.data.houseData.house_types.map(item => {
                    item.change = false;
                    return item
                }),
                ['houseData.room_types']: this.data.houseData.room_types.map(item => {
                    item.change = false;
                    return item
                }),
                roomNum: this.data.roomNum.map(item => {
                    item.change = false;
                    return item
                })
            })
            this.changeFilter()
        },
        // 搜索
        search() {
            this.searchRequest.reset({
                city: this.data.city,
                start_live_time: this.data.start,
                end_live_time: this.data.end,
                title: this.data.value,
                order: this.data.sortType,
                sort: this.data.sort,
                house_type: this.data.houseData.house_types.filter(item => item.change).map(item => item.value),
                room_type: this.data.houseData.room_types.filter(item => item.change).map(item => item.value),
                room_count: this.data.roomNum.filter(item => item.change).map(item => item.value),
            })
        },
        onReachBottom() {
            this.searchRequest.next()
        }
    }
});
